package com.ephuizi.excel;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;

public class ExcelOperate {

	public Logger logger = Logger.getLogger(ExcelOperate.class);

	public void ExploreOrder(String savePath) {
		if (!ExcelConstantField.fieldArrayVerify()) {
			logger.error("ExcelConstantField 或者 RecordRow 代码中字段对应关系不正确");
			return;
		}

		String sheetName = "订单信息表";
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet(sheetName);

		initColumnField(workbook, sheet);
		RecordRow recordRow = new RecordRow();
		addRowRecord(workbook, sheet, recordRow);

		FileOutputStream out;
		try {
			File test = new File(savePath);
			if (!test.exists()) {
				test.createNewFile();
			}
			out = new FileOutputStream(test);
			workbook.write(out);
			out.close();
			System.out.println("fontstyle.xls written successfully");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 初始化表单列名和列宽
	 * 
	 * @param workbook
	 * @param sheet
	 */
	private void initColumnField(HSSFWorkbook workbook, HSSFSheet sheet) {
		short COLUMN_NAME_HEIGHT = 14;

		HSSFRow row = sheet.createRow(0);
		// 第一行字段名
		String[] fieldNames = ExcelConstantField.fieldNameToArrayBySort();
		Field[] ecfFields = ExcelConstantField.class.getFields();
		for (int i = 0; i < fieldNames.length; i++) {

			try {
				row.createCell(i).setCellValue(String.valueOf(ecfFields[i].get(ExcelConstantField.class)));
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}

		// 设置列宽
		int[] fieldWidthArr = ExcelConstantField.fieldWidthArray();
		for (int i = 0; i < fieldWidthArr.length; i++) {
			sheet.setColumnWidth(i, fieldWidthArr[i]);
		}

		// 字体样式
		HSSFFont fieldFont = workbook.createFont();
		fieldFont.setFontName("黑体");
		fieldFont.setFontHeightInPoints(COLUMN_NAME_HEIGHT);// 字体大小
		fieldFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);// 加粗
		// Set font into style
		HSSFCellStyle fieldStyle = workbook.createCellStyle();
		fieldStyle.setFont(fieldFont);
		row.setRowStyle(fieldStyle);

		for (int i = 0; i < fieldNames.length; i++) {
			row.getCell(i).setCellStyle(fieldStyle);
		}

	}

	public void addRowRecord(HSSFWorkbook workbook, HSSFSheet sheet, RecordRow recordRow) {

		Row row = sheet.createRow(sheet.getLastRowNum() + 1);
		String[] fieldNames = ExcelConstantField.fieldNameToArrayBySort();
		Field[] rrFields = RecordRow.class.getFields();
		for (int i = 0; i < fieldNames.length; i++) {
			try {
				row.createCell(i).setCellValue(String.valueOf(rrFields[i].get(recordRow)));
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
	}

}
